﻿using System.Collections.Generic;

namespace MiscHelper
{
    public static class EmailFieldParser
    {
        public static List<string> Parse(string sz)
        {
            var ret = new List<string>();
            if (string.IsNullOrEmpty(sz))
                return ret;

            sz = sz.Trim().ToLower();
            if (string.IsNullOrEmpty(sz))
                return ret;

            // delimiters are not standardised!
            sz = sz.Replace('/', ',');
            sz = sz.Replace(';', ',');
            string[] sa = sz.Split(',');
            foreach (var emailAddress in sa)
            {
                AddSanitizedEmail(emailAddress, ret);
            }
            return ret;
        }

        private static void AddSanitizedEmail(string emailAddress, List<string> ret)
        {
            emailAddress = emailAddress.Trim();

            // avoid incomprehensible regex!
            if (emailAddress.Contains(".") && emailAddress.Contains("@"))
            {
                ret.Add(emailAddress);
            }
        }
    }
}
